package com.example.victory.filter;

import com.cgws.fintech.sdk.common.web.WebHelper;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * feign接口调用设置，添加header信息
 */
@Configuration
public class FeginClientConfig implements RequestInterceptor {
    @Value("${spring.application.name:}")
    private String appName;

    @Override
    public void apply(RequestTemplate template) {
        String sessionid = WebHelper.getSessionId();
        if (!StringUtils.isEmpty(sessionid)) {
            template.header("sessionid", sessionid);
        }
        HttpServletRequest request = WebHelper.getHttpServletRequest();
        if (request != null) {
            String ticket = request.getHeader("ticket");
            if (!StringUtils.isEmpty(ticket)) {
                template.header("ticket", ticket);
            }
        }
        // 内部调用，header中填写调用方的服务名称
        if (!StringUtils.isEmpty(appName)) {
            template.header("service", appName);
        }
    }
}
